<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>加密过滤器</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filters.compression.html">压缩过滤器</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="transports.html">所支持的套接字传输器（Socket Transports）列表</a></div>
 <div class="up"><a href="filters.html">可用过滤器列表</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="filters.encryption" class="section">
    <h2 class="title">加密过滤器</h2>
    <p class="simpara">
    <em>mcrypt.*</em>和 
    <em>mdecrypt.*</em>使用 libmcrypt 提供了对称的加密和解密。这两组过滤器都支持 
    <a href="ref.mcrypt.html" class="link">mcrypt 扩展库</a>中相同的算法，格式为 
    <em>mcrypt.ciphername</em>，其中 
    <em><code class="parameter">ciphername</code></em>是密码的名字，将被传递给 
    <span class="function"><a href="function.mcrypt-module-open.html" class="function">mcrypt_module_open()</a></span>。有以下五个过滤器参数可用：</p>
    <p class="para">
      <table class="doctable table">
        <caption><strong>mcrypt 过滤器参数</strong></caption>
        
          <thead>
            <tr>
              <th>参数</th>
              <th>是否必须</th>
              <th>默认值</th>
              <th>取值举例</th>
            </tr>

          </thead>

          <tbody class="tbody">
            <tr>
              <td>mode</td>
              <td>可选</td>
              <td>cbc</td>
              <td>cbc, cfb, ecb, nofb, ofb, stream</td>
            </tr>

            <tr>
              <td>algorithms_dir</td>
              <td>可选</td>
              <td>ini_get(&#039;mcrypt.algorithms_dir&#039;)</td>
              <td>algorithms 模块的目录</td>
            </tr>

            <tr>
              <td>modes_dir</td>
              <td>可选</td>
              <td>ini_get(&#039;mcrypt.modes_dir&#039;)</td>
              <td>modes 模块的目录</td>
            </tr>

            <tr>
              <td>iv</td>
              <td>必须</td>
              <td>N/A</td>
              <td>典型为 8，16 或 32 字节的二进制数据。根据密码而定</td>
            </tr>

            <tr>
              <td>key</td>
              <td>必须</td>
              <td>N/A</td>
              <td>典型为 8，16 或 32 字节的二进制数据。根据密码而定</td>
            </tr>

          </tbody>
        
      </table>

    </p>
    <div class="example" id="example-5518">
      <p><strong>Example #1 用 3DES 将文件加密输出</strong></p>
      <div class="example-contents">
        <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$passphrase&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'My&nbsp;secret'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Turn&nbsp;a&nbsp;human&nbsp;readable&nbsp;passphrase<br />&nbsp;*&nbsp;into&nbsp;a&nbsp;reproducable&nbsp;iv/key&nbsp;pair<br />&nbsp;*/<br /></span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass1'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass2'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">24</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'key'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'secert-file.enc'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'wb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mcrypt.tripledes'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Secret&nbsp;secret&nbsp;secret&nbsp;data'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
</div>

    </div>
    <div class="example" id="example-5519">
      <p><strong>Example #2 读取加密的文件</strong></p>
      <div class="example-contents">
        <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$passphrase&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'My&nbsp;secret'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Turn&nbsp;a&nbsp;human&nbsp;readable&nbsp;passphrase<br />&nbsp;*&nbsp;into&nbsp;a&nbsp;reproducable&nbsp;iv/key&nbsp;pair<br />&nbsp;*/<br /></span><span style="color: #0000BB">$iv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass1'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">md5</span><span style="color: #007700">(</span><span style="color: #DD0000">'pass2'</span><span style="color: #007700">.</span><span style="color: #0000BB">$passphrase</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">24</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'iv'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$iv</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'key'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'secert-file.enc'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rb'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">stream_filter_append</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mdecrypt.tripledes'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_FILTER_WRITE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">stream_get_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
</div>

    </div>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="filters.compression.html">压缩过滤器</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="transports.html">所支持的套接字传输器（Socket Transports）列表</a></div>
 <div class="up"><a href="filters.html">可用过滤器列表</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
